﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Configuration;

namespace CFSystem.JXC
{
    
    /// <summary>
    /// Client to use the Factory entity.
    /// </summary>
    public class ExecuteDB
    {
        private static string connectionString;
        private static string dbType;

        public ExecuteDB() { }

        /**/
        /// <summary>數據庫連接字符串</summary> 
        public static string ConnectionString
        {
            get
            {
                if (connectionString == null) connectionString = ConfigurationSettings.AppSettings["ConnectionString"];
                return connectionString;
            }
            set { connectionString = value; }
        }

        /**/
        /// <summary>數據庫類型</summary> 
        public static string DBType
        {
            get
            {
                if (dbType == null) dbType = ConfigurationSettings.AppSettings["DataBaseType"];
                return dbType;
            }
            set { dbType = value; }
        }

        /**/
        /// <summary>執行SQL語句返回DataSet</summary> 
        public static DataSet ExcuteSql(string sqlString)
        {
            DataSet ds = new DataSet();
            ConnectionFactory objConn = SimpleFactory.GetConnFactory(ConnectionString, DBType);
            IDbConnection iConn = objConn.GetConnection();
            CommandFactory objComm = SimpleFactory.GetCommFactory(iConn, sqlString, DBType);
            IDbCommand iComm = objComm.GetCommand();

            try
            {
             
                iConn.Open();
                DataAdapterFactory objAdapter = SimpleFactory.GetDataAdapterFactory(iComm, DBType);
                IDbDataAdapter dataAdaper = objAdapter.GetDataAdapter();
                dataAdaper.Fill(ds);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                iComm.Dispose();
                iConn.Close();
                iConn.Dispose();
            }

            return ds;
        }
    } 

}
